home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / GRAPHICS.SWG / 0073_RIP Bezier Curves.pas < prev    next >
Pascal/Delphi Source File  |  1994-02-03  |  3KB  |  96 lines

  1. {
  2. From: SEAN PALMER
  3. Subj: RIP Bezier Curve
  4. ---------------------------------------------------------------------------
  5.  NO> Does anyone have any code for constructing a RIP Bezier curve that is
  6.  NO> exactly the same as the one used by Telegrafix developers. I have some
  7.  NO> code that comes close, but close isn't good enough. I need this to be
  8.  NO> dead on accurate.
  9.  NO> PS. I'm willing to share my code with others that are interested in
  10.  NO> RIP.
  11.  
  12. {Public domain by Sean Palmer}
  13. {converted from Steve Enns' original Basic subroutines by Sean Palmer}
  14.  
  15. var color:byte;
  16. procedure plot(x,y:word);begin
  17.  mem[$A000:y*320+x]:=color;
  18.  end;
  19.  
  20. type
  21.  coord=record x,y:integer; end;
  22.  CurveDataRec=array[0..65521 div sizeof(coord)]of coord;
  23.  
  24. procedure drawBSpline(var d0:coord;nPoints,nSteps:word);
  25.  const nsa=1/6; nsb=2/3;
  26.  var
  27.   i,i2,xx,yy:integer;
  28.   t,ta,t2,t2a,t3,t3a,nc1,nc2,nc3,nc4,step:real;
  29.   d:curveDataRec absolute d0;
  30. begin
  31.  step:=1/nSteps;
  32.  for i:=0 to nPoints-4 do begin
  33.   color:=i+32+2;
  34.   t:=0.0;
  35.   for i2:=pred(nSteps)downto 0 do begin
  36.    t:=t+step;
  37.    ta:=t*0.5; t2:=t*t; t2A:=t2*0.5; t3:=t2*t; t3A:=t3*0.5;
  38.    nc1:=-nsa*t3+t2A-ta+nsa;
  39.    nc2:=t3a-t2+nsb;
  40.    nc3:=-t3a+t2a+ta+nsa;
  41.    nc4:=nsa*t3;
  42.    xx:=round(nc1*d[i].x+nc2*d[succ(i)].x+nc3*d[i+2].x+nc4*d[i+3].x);
  43.    yy:=round(nc1*d[i].y+nc2*d[succ(i)].y+nc3*d[i+2].y+nc4*d[i+3].y);
  44.    plot(xx,yy);
  45.    end;
  46.   end;
  47.  end;
  48.  
  49. procedure drawBezier(var d0:coord;nPoints,nSteps:word);
  50.  const nsa=1/6; nsb=2/3;
  51.  var
  52.   i,i2,i3,xx,yy:integer;
  53.   t,tm3,t2,t2m3,t3,t3m3,nc1,nc2,nc3,nc4,step:real;
  54.   d:curveDataRec absolute d0;
  55. begin
  56.  step:=1/nSteps;
  57.  for i2:=0 to pred(nPoints) div 4 do begin
  58.   i:=i2*4;
  59.   t:=0.0;
  60.   for i3:=pred(nSteps) downto 0 do begin
  61.    t:=t+step;
  62.    tm3:=t*3.0; t2:=t*t; t2m3:=t2*3.0; t3:=t2*t; t3m3:=t3*3.0;
  63.    nc1:=1-tm3+t2m3-t3;
  64.    nc2:=t3m3-2.0*t2m3+tm3;
  65.    nc3:=t2m3-t3m3;
  66.    nc4:=t3;
  67.  
  68.    xx:=round(nc1*d[i].x+nc2*d[succ(i)].x+nc3*d[i+2].x+nc4*d[i+3].x);
  69.    yy:=round(nc1*d[i].y+nc2*d[succ(i)].y+nc3*d[i+2].y+nc4*d[i+3].y);
  70.    plot(xx,yy);
  71.    end;
  72.   end;
  73.  end;
  74.  
  75. const numpoints=40;
  76.  
  77. var c:array[-1..2+numPoints]of coord;
  78. var i:integer;
  79. begin
  80.  asm mov ax,$13; int $10; end;  {init vga/mcga graphics}
  81.  randomize;
  82.  for i:=1 to numPoints do with c[i] do begin
  83.   x:=i*(319 div numPoints);    {for precision demo}
  84.  {x:=random(320);}             {for fun demo}
  85.   y:=random(200);
  86.   end;
  87.  for i:=1 to numPoints div 2 do c[i*2+1].y:=c[i*2].y;    {fit closer}
  88.  for i:=1 to numPoints do with c[i] do begin color:=i+32; plot(x,y); end;
  89.  c[-1]:=c[1]; c[0]:=c[1];  {replicate end points so curves fit to input}
  90.  c[numPoints+1]:=c[numPoints]; c[numPoints+2]:=c[numPoints];
  91.  drawBSpline(c[-1],numPoints+4,256); {set third parm to 256 for precision, 64 f}
  92.  readln;
  93.  asm mov ax,3; int $10; end;  {text mode again}
  94.  end.
  95.  
  96.